<template>
  <!--短信下发新增/编辑弹窗-->
  <a-modal  :maskClosable="false"
            :title="mdl.dialogTitleMap[mdl.dialogStatus]"
            :width="1000"
            @ok="mdl.dialogStatus === 'create' ? createData() : updateData()"
            v-model="mdl.visible" :footer="null"
  >
    <a-card :bordered="false"  :bodyStyle="{padding : '10px 10px'}">

      <a-layout class="transparent" style="margin-bottom: 24px">

        <a-layout  style="margin-right: 15px;">
          <a-layout-content class="border" style="margin: 0">

            <a-card :bordered="false">
              <a-form-item label="选择短信类别" :labelCol="mdl.labelCol"
                           :wrapperCol="mdl.wrapperCol">
                <a-select v-model="mdl.formData.id" allowClear style="width: 200px">
                  <a-select-option value="1">考试预约</a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item
                :labelCol="mdl.labelCol"
                :wrapperCol="mdl.wrapperCol"
                label="选择接收人">
                <!--<a-select
                  mode="multiple"
                  size="default"
                  placeholder="请选择接收人"
                  v-model="mdl.formData.receiver"
                  style="width: 200px"
                  @change="handleChange"
                  @popupScroll="popupScroll"
                >
                  <a-select-option
                    :key="item.key"
                    :value="item.value"
                    v-for="item in options">
                    {{ item.label }}
                  </a-select-option>
                </a-select>-->
                <treeselect
                  :multiple="true"
                  :options="options"
                  :flat="true"
                  :default-expand-level="1"
                  placeholder="请选择接收人"
                  v-model="mdl.formData.receiver"
                  noChildrenText="暂无数据!"
                />

              </a-form-item>
              <a-textarea style="margin-bottom: 24px;"  rows="8" v-model="searchParams.msgContent">
              </a-textarea>
              <div class="center">
                <a-button type="primary"  @click="sendSmsByArea" :loading="loadingMore" style="width: 200px">发送信息</a-button>
              </div>

            </a-card>
          </a-layout-content>
        </a-layout>
      </a-layout>
    </a-card>
  </a-modal>
</template>
<script>
/* eslint-disable */
  import {queryCompanyUserRel,querySmsTemplate,insertInfoSmsDelivery} from "@/api/messageManage/sendingSMS"
  import { STable } from '@/components'
  import AInput from "ant-design-vue/es/input/Input";

  // import the component
  // import Treeselect from '@/riophae/vue-treeselect'
  // import the styles
  // import '@riophae/vue-treeselect/dist/vue-treeselect.css'


  export default {
    components: {
      AInput,
      STable,
      // Treeselect
    },
    props: {
      /*   -------弹窗--------*/
      mdl: {
        dialogStatus: '',
        dialogTitleMap: {
          update: '编辑',
          create: '新增'
        },
        visible: false,
        title: '',
        formData: {
          createDept: '',
          createTime: '',
          createUser: '',
          id: '',
          isDeleted: '',
          msgContent: '',
          msgType: 0,
          phoneNum: '',
          receiver: '',
          senderInfo: '',
          status: '',
          updateTime: '',
          updateUser: '',
          userName:""
        },
        labelCol: {
          xs: {
            span: 24
          }
          ,
          sm: {
            span: 5
          }
        }
        ,
        wrapperCol: {
          xs: {
            span: 24
          }
          ,
          sm: {
            span: 16
          }
        },
      },
    },
    data() {
      return {
        options:[],
        index:0,
        /*   -------搜索框--------*/
        searchParams: {
          smsTemplateId:null,
          areaId:null,
          serviceId: null,
          msgContent:""
        },
        phone:"",
        loadingMore:false,
        content:"",//【南京市环境保护局】尊敬的[1000085]胡002:南京市环境保护局提醒您 请于2020.10.10 9:00:00准时参加《垃圾分类》的考试!
        info:{},
      }
    },
    inject: ['upShow'],

    created() {
      this.queryCompanyUserRelData();
    },
    watch: {

    },
    computed: {

    },
    mounted() {
      this.querySmsTemplateData();
    },
    methods: {
      handleChange(value) {
        console.log(`Selected: ${value}`);
      },
      popupScroll() {
        console.log('popupScroll');
      },
//          新增
      createData() {
        const parameter = this.mdl.formData
        createRefundRecord(parameter).then(res => {
          if(res.code === 200
          )
          {
            this.upShow(1,"添加数据")
            this.mdl.visible = false
            this.handleSearch()
          }
          else
          {
            this.upShow(2,res.msg)
          }

        }).
        catch(err => {
          this.upShow(2)
        })
      },
//      编辑
      updateData() {

      },
      changeContent(value) {
        console.log('value:' + value);
        this.content = value
      },
      sendSmsByPhone(){
        let param ={
          phone:this.phone,
          content:this.content
        };
        sendSmsByPhone(param).then(res=>{
          this.$success({
            title: '通知',
            content: '发送成功!',
          });
        })
      },
      sendSmsByArea(){
        let param ={
          createDept: '',
          createTime: '',
          createUser: '',
          id: this.mdl.formData.id,
          isDeleted: '',
          msgContent: this.searchParams.msgContent,
          msgType: 0,
          phoneNum: '',
          receiver: this.mdl.formData.receiver.toString(),
          senderInfo: '',
          status: '',
          updateTime: '',
          updateUser: '',
          userName:""
        };
        this.loadingMore=true;
        insertInfoSmsDelivery(param).then(res=>{
          this.loadingMore=false
          if(res.code === 200){
            this.$success({
              title: '通知',
              content: '发送成功!',
            });
            this.mdl.visible = false;
            this.$emit("handleSearch");
          }
        }).catch(e=>{
          this.$error({
            title: '通知',
            content: '发送失敗!',
          });
          this.loadingMore=false
        })
      },
      // 短信下发查询接收人 queryCompanyUserRel
      queryCompanyUserRelData() {
        queryCompanyUserRel().then(res => {
          if(res.code === 200){
            let treeData=[
              {
                id:'企业用户',
                label: '企业用户',
                children:[
                  {
                    id:'重点排污企业',
                    label: '重点排污企业',
                    children:[]
                  },
                  {
                    id:'其他企业',
                    label: '其他企业',
                    children:[]
                  }
                ]
              },
              {
                id:'非企业用户',
                label: '非企业用户',
                children:[
                  {
                    id:'学校老师用户',
                    label: '学校老师用户',
                    children:[]
                  },
                  {
                    id:'学校学生用户',
                    label: '学校学生用户',
                    children:[]
                  },
                  {
                    id:'环保局用户',
                    label: '环保局用户',
                    children:[]
                  },
                  {
                    id:'个人用户',
                    label: '个人用户',
                    children:[]
                  }
                ]
              }
            ];
            res.data.map((item, inx) => {
              item.label = item.userName;
              item.id = inx;
              if(item.userType === 0){//0:企业用户
                item.userTypeTxt = "企业用户";
                if(item.companyType === 0){//0:重点排污企业;
                  item.companyTypeTxt = "重点排污企业";
                  treeData[0].children[0].children.push(item);
                }else if(item.companyType === 1){//1:其他企业
                  item.companyTypeTxt = "其他企业";
                  treeData[0].children[1].children.push(item);
                }
              }else{//非企业用户  1:学校学生用户;2:学校老师用户;3:个人用户;4:环保局用户
                item.userTypeTxt = "非企业用户";
                if(item.userType === 1){//学校学生用户
                  item.userTypeTxt = "学校学生用户";
                  treeData[1].children[1].children.push(item);
                }else if(item.userType === 2){//学校老师用户
                  item.userTypeTxt = "学校老师用户";
                  treeData[1].children[0].children.push(item);
                }else if(item.userType === 3){//个人用户
                  item.userTypeTxt = "个人用户";
                  treeData[1].children[3].children.push(item);
                }else if(item.userType === 4){//环保局用户
                  item.userTypeTxt = "环保局用户";
                  treeData[1].children[2].children.push(item);
                }
              }
            })

            console.log("treeData",treeData)
            this.options = treeData;
          }
        }).
        catch(err => {
          console.log(err)
        })
      },
      //查询短信模板querySmsTemplate
      querySmsTemplateData() {
        let params = {
          _search: true,
          bgTm:"",
          createBy:"",
          data:{},
          endTm:"",
          keywords:"",
          pageSize:"",
          pageNumber:"",
          selected:"",
          selectedType:"",
          sortField:"",
          sortOrder:"",
          status:"",
          type:""
        }
        querySmsTemplate(params).then(res => {
          if(res.code === 200){
            this.searchParams.msgContent = res.data;
          }
        }).
        catch(err => {
          console.log(err)
        })
      },
    }
  }
</script>
<style lang="less" scoped>

  .operationBtn{
    height: 36px;
    line-height: 36px;
    font-size: 16px;
    text-align: center;
    // background: #39a1ea;
    color: #fff;
    border-radius: 0;
  }

  .border {
    border: 1px solid #e8e8e8;
  }

  .title {
    color: rgba(0, 0, 0, .85);
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 16px;
  }

  .transparent {

    background-color: #fff;


    .ant-layout , .ant-layout-sider, .ant-layout-header {
      background: #ffffff;
    }


    .ant-layout-sider {
      flex: 0 0 450px;
      max-width: 450px;
      min-width: 450px;
      width: 450px;
    }

    .ant-layout-header {
      height: auto;
      padding: 0;
      /*line-height: 64px;*/
      /*background: #001529;*/
    }
  }

  #asideProfile {
    dl,dt,dd {
      padding: 0;
      margin: 0;
    }

    dd{
      font-weight: 700
    }

    .item-tiling>* {
      width: 100%;
    }

    .d-flex {
      display: -webkit-box !important;
      display: -ms-flexbox !important;
      display: flex !important;
    }

    .profile-intro {
      border-bottom: 1px solid #e3e3e3;
    }

    .data-info {
      padding: 12px 10px;
      border-bottom: 1px solid #e3e3e3;
      text-align: center;
    }

    .grade-box {
      margin-top: -8px;
      padding: 16px;
      text-align: center;

      dl dt {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-pack: justify;
        -ms-flex-pack: justify;
        justify-content: space-between;
      }

      dd{
        margin-left: 6px;
      }

      dl {
        margin-top: 8px;
        float: left;
      }

      dl:nth-child(even) {
        width: 45%;
      }

      dl:nth-child(odd) {
        width: 55%;
      }
    }

  }








  #asideProfile .grade-box dl dd {
    font-weight: 600;
    margin-left: 6px;
  }

  #asideProfile .grade-box dl {
    margin-top: 8px;
    float: left;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
  }


  .tableBox {
    width: 100%;
    margin: auto;
    border: solid 1px #e5e5e5;
    height: 40px;
    background: #fff;
    margin-bottom: 6px;

    .tableBox-item {
      float: right;
      line-height: 40px;
      border-left: solid 1px #e5e5e5;
      padding-left: 12px;
      padding-right: 12px;
      cursor: pointer;
    }

    .tableBox-button {
      height: 40px; line-height: 40px; padding-left: 10px; color: #39a1ea;
    }
  }

</style>
<style lang="less">




  .fixed-width-label  .ant-form-item-label {
    width: 100px !important;
  }

  .text-red {
    label {
      color: red ;
    }
    input{
      border-color: red;
    }

  }

  .custom-tree {

    /deep/ .ant-menu-item-group-title {
      position: relative;

      &:hover {
        .btn {
          display: block;
        }
      }
    }

    /deep/ .ant-menu-item {
      &:hover {
        .btn {
          display: block;
        }
      }
    }

    /deep/ .btn {
      display: none;
      position: absolute;
      top: 0;
      right: 10px;
      width: 20px;
      height: 40px;
      line-height: 40px;
      z-index: 1050;

      &:hover {
        transform: scale(1.2);
        transition: 0.5s all;
      }
    }
  }

  .vue-treeselect div{
    line-height:40px;
  }
</style>
